package com.gitee.ricesugar.book;

import com.gitee.ricesugar.book.module.AddBook;
import com.gitee.ricesugar.book.module.ListBook;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class BookPanel {
    private static JPanel contentPanel;

    public static JPanel createBookPanel() throws IOException {
        //新建主布局
        JPanel bookPanel = new JPanel();
        bookPanel.setLayout(new BorderLayout());

        //添加侧边栏
        JPanel sidebar = new JPanel();
        sidebar.setLayout(new GridLayout(2, 1));

        JButton addBookButton = new JButton("添加图书");
        JButton bookListButton = new JButton("图书列表");
        addBookButton.setPreferredSize(new Dimension(96, 40));

        sidebar.add(addBookButton);
        sidebar.add(bookListButton);

        //添加每个页面
        contentPanel = new JPanel();
        contentPanel.setLayout(new CardLayout());
        JPanel addBookPage = new AddBook();
        JPanel bookListPage = new ListBook();
        contentPanel.add(addBookPage, "addBookPage");
        contentPanel.add(bookListPage, "bookListPage");
        showPanel("bookListPage");

        addBookButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showPanel( "addBookPage");
            }
        });
        bookListButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showPanel("bookListPage");
            }
        });

        bookPanel.add(sidebar, BorderLayout.WEST);
        bookPanel.add(contentPanel, BorderLayout.CENTER);

        //返回主布局
        return bookPanel;
    }

    private static void showPanel(String panelName) {
        CardLayout cardLayout = (CardLayout) contentPanel.getLayout();
        cardLayout.show(contentPanel, panelName);
    }
}
